<?php

require_once './library/Zend/Db/Table/Abstract.php';

class TableDetail extends Zend_Db_Table_Abstract
{

    protected $_name;
    protected $_primary;
    protected $_db;
    protected $_registery;
    private $_select;
    protected $_assName;

    protected function _setup()
    {
        $this->_name = 'detail';
        $this->_primary = 'IdDetail';
        $this->_assName = 'ass_compodetail';
        $this->_registry = Zend_Registry::getInstance();
        $this->_db = $this->_registry->get('adapter');
        parent::_setup();
    }

    public function getDetail($pIdComposant)
    {
        $this->_select = $this->_db->select();
        $this->_select->from(array('d' => $this->_name));
        $this->_select->join(array('a' => $this->_assName), 'a.IdDetail = d.IdDetail', array());
        $this->_select->where('a.IdComposant = ?', $pIdComposant);

        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result))
            return $result;
    }

    public function addDetail($pIdComposant, $pSpeedInterface, $pMacInterface, $pIpInterface, $pMaskInterface, $pStatusInterface, $pDescriptionInterface)
    {
        $val = array('SpeedInterface' => $pSpeedInterface,
            'MacInterface' => $pMacInterface,
            'IpInterface' => $pIpInterface,
            'MaskInterface' => $pMaskInterface,
            'StatusInterface' => $pStatusInterface,
            'DescriptionInterface' => $pDescriptionInterface);
        $this->_db->insert($this->_name, $val);
        $idDetail = $this->_db->lastInsertId();

        $this->assDetail($pIdComposant, $idDetail);
    }

    private function assDetail($pIdComposant, $idDetail)
    {
        $assVal = array($this->_primary => $idDetail,
            'IdComposant' => $pIdComposant);
        $this->_db->insert($this->_assName, $assVal);
    }

    public function deleteDetail($pIdComposant, $pIdDetail)
    {
        $this->delete($this->_db->quoteInto('IdDetail = ?', $pIdDetail));

        $this->_name = $this->_assName;
        $c1 = $this->_db->quoteInto('IdDetail = ?', $pIdDetail);
        $c2 = $this->_db->quoteInto('IdComposant = ?', $pIdComposant);
        $this->delete($c1, $c2);
        $this->_name = 'detail';
    }

    public function updateDetail($pIdDetail, $pSpeedInterface, $pMacInterface, $pIpInterface, $pMaskInterface, $pStatusInterface, $pDescriptionInterface)
    {
        $val = array('SpeedInterface' => $pSpeedInterface,
            'MacInterface' => $pMacInterface,
            'IpInterface' => $pIpInterface,
            'MaskInterface' => $pMaskInterface,
            'StatusInterface' => $pStatusInterface,
            'DescriptionInterface' => $pDescriptionInterface);

        $this->update($val, $this->_db->quoteInto('IdDetail = ?', $pIdDetail));
    }

}
